import { computed, ref } from 'vue'
import { defineStore } from 'pinia'

export const STORAGE_AUTHORIZE_KEY = 'training-center-authorization'

export const useTokenStore = defineStore('token', () => {
  const token = ref<string | null>(null)

  const bearerToken = computed(() => (token.value ? `Bearer ${token.value}` : ''))

  function setToken(newToken: string | null) {
    token.value = newToken
  }

  function removeToken() {
    token.value = null
  }

  return {
    token,
    bearerToken,
    setToken,
    removeToken,
  }
}, {
  persist: {
    key: STORAGE_AUTHORIZE_KEY,
  },
})
